home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Technology Seed / Jan. '98 ATS.toast / QuickTime™ 3.0b11 / QTPublicInterfaces / CIncludes / Math64.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-12  |  11.7 KB  |  445 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Math64.h
  3.  
  4.      Contains:    64-bit integer math Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    QuickTime 3.0 Beta
  8.  
  9.      Copyright:    © 1994-1997 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. */
  18. #ifndef __MATH64__
  19. #define __MATH64__
  20.  
  21. #ifndef __MACTYPES__
  22. #include <MacTypes.h>
  23. #endif
  24.  
  25. #if TARGET_OS_MAC
  26. #include <limits.h>
  27. #endif
  28.  
  29.  
  30. #if PRAGMA_ONCE
  31. #pragma once
  32. #endif
  33.  
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37.  
  38. #if PRAGMA_IMPORT
  39. #pragma import on
  40. #endif
  41.  
  42. #if PRAGMA_STRUCT_ALIGN
  43.     #pragma options align=mac68k
  44. #elif PRAGMA_STRUCT_PACKPUSH
  45.     #pragma pack(push, 2)
  46. #elif PRAGMA_STRUCT_PACK
  47.     #pragma pack(2)
  48. #endif
  49.  
  50.  
  51. /*--------------------------------------------------------------------------------
  52.                 These routines are intended to provide C software support for
  53.                 64 bit integer types.  Their behavior should mimic anticipated
  54.                 64 bit hardware. This implementation should replace use of the
  55.                 "wide" type found in PowerPC.
  56.  
  57.     The following routines are available for performing math on 64-bit integers:
  58.     
  59.     S64Max
  60.                 Returns the largest representable SInt64.
  61.     S64Min
  62.                 Returns the smallest (i.e. most negative) SInt64.  Note: the negative
  63.                 (absolute value) of this number is not representable in an SInt64.
  64.                 That means that S64Negate(S64Min) is not representable (in fact,
  65.                 it returns S64Min).
  66.     S64Add
  67.                 Adds two integers, producing an integer result.  If an overflow
  68.                 occurs the result is congruent mod (2^64) as if the operands and
  69.                 result were unsigned.  No overflow is signaled.
  70.     
  71.     S64Subtract
  72.                 Subtracts two integers, producing an integer result.  If an overflow
  73.                 occurs the result is congruent mod (2^64) as if the operands and
  74.                 result were unsigned.  No overflow is signaled.
  75.  
  76.     S64Negate
  77.                 Returns the additive inverse of a signed number (i.e. it returns
  78.                 0 - the number).  S64Negate (S64Min) is not representable (in fact,
  79.                 it returns S64Min).
  80.     
  81.     S64Absolute
  82.                 Returns the absolute value of the number (i.e. the number if
  83.                 it is positive, or 0 - the number if it is negative).
  84.                 See S64Negate above.
  85.                 
  86.     S64Multiply
  87.                 Multiplies two signed numbers, producing a signed result.  Overflow
  88.                 is ignored and the low-order part of the product is returned.  The
  89.                 sign of the result is not guaranteed to be correct if the magnitude
  90.                 of the product is not representable.
  91.     S64Divide
  92.                 Divides dividend by divisor, returning the quotient.  The remainder
  93.                 is returned in *remainder if remainder (the pointer) is non-NULL.
  94.                 The sign of the remainder is the same as the sign of the dividend
  95.                 (i.e. it takes the absolute values of the operands, does the division,
  96.                 then fixes the sign of the quotient and remainder).  If the divisor
  97.                 is zero, then S64Max() will be returned (or S64Min() if the dividend
  98.                 is negative), and the remainder will be the dividend; no error is
  99.                 reported.
  100.     
  101.     S64Set
  102.                 Given an SInt32, returns an SInt64 with the same value.  Use this
  103.                 routine instead of coding 64-bit constants (at least when the
  104.                 constant will fit in an SInt32).
  105.     
  106.     S64SetU
  107.                 Given a UInt32, returns a SInt64 with the same value.
  108.     
  109.     S64Compare
  110.                 Given two signed numbers, left and right, returns an
  111.                 SInt32 that compares with zero the same way left compares with
  112.                 right.  If you wanted to perform a comparison on 64-bit integers
  113.                 of the form:
  114.                         operand_1 <operation> operand_2
  115.                 then you could use an expression of the form:
  116.                         xxxS64Compare(operand_1,operand_2) <operation> 0
  117.                 to test for the same condition.
  118.                 
  119.                 CAUTION: DO NOT depend on the exact value returned by this routine.
  120.                 Only the sign (i.e. positive, zero, or negative) of the result is
  121.                 guaranteed.
  122.  
  123.     S64And, S64Or, S64Eor and S64Not
  124.     
  125.                 Return Boolean (1 or 0) depending on the outcome of the logical
  126.                 operation.
  127.  
  128.     S64BitwiseAnd, S64BitwiseOr, S64BitwiseEor and S64BitwiseNot
  129.     
  130.                 Return the Bitwise result.
  131.                 
  132.     S64ShiftRight and S64ShiftLeft
  133.     
  134.                 The lower 7 bits of the shift argument determines the amount of 
  135.                 shifting.  S64ShiftRight is an arithmetic shift while U64ShiftRight
  136.                 is a logical shift.
  137.  
  138.     SInt64ToLongDouble
  139.                 
  140.                 Converts SInt64 to long double.  Note all SInt64s fit exactly into 
  141.                 long doubles, thus, the binary -> decimal conversion routines
  142.                 in fp.h can be used to achieve SInt64 -> long double -> decimal
  143.                 conversions.
  144.                 
  145.     LongDoubleToSInt64
  146.     
  147.                 Converts a long double to a SInt64.  Any decimal string that fits
  148.                 into a SInt64 can be converted exactly into a long double, using the
  149.                 conversion routines found in fp.h.  Then this routine can be used
  150.                 to complete the conversion to SInt64.
  151.                 
  152.     SInt64ToWide
  153.     
  154.                 Converts a SInt64 to a wide struct.  If SInt64 is implemented
  155.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  156.                 implememnted as a long long, it casts the long long into a 
  157.                 wide struct.
  158.     
  159.     WideToSInt64
  160.     
  161.                 Converts a wide struct into a SInt64.  If SInt64 is implemented
  162.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  163.                 implememnted as a long long, it reads the struct into a long long.
  164.     
  165.     
  166.     The corresponding UInt64 routines are also included.
  167.     
  168. --------------------------------------------------------------------------------*/
  169.  
  170.  
  171. #if TYPE_LONGLONG 
  172.  
  173. #define S64Max() LLONG_MAX
  174. #define S64Min() LLONG_MIN
  175. #define S64Add(x, y) ((x) + (y))
  176. #define S64Subtract(x, y) ((x) - (y))
  177. #define S64Negate(x) (-(x))
  178. #define S64Absolute(x) absll((x))
  179. #define S64Multiply(x, y) ((x) * (y))
  180. #define S64Div(x, y) ((x) / (y))
  181. #define S64Mod(x, y) ((x) % (y))
  182. #define S64Set(x) ((SInt64) (x))
  183. #define S64SetU(x) ((SInt64) (x))
  184. #define S32Set(x) ((SInt32) (x))
  185. #define S64Compare(x, y) ((int)((x) - (y)))
  186. #define S64And(x, y) ((Boolean)((x) && (y)))
  187. #define S64Or(x, y) ((Boolean)((x) || (y)))
  188. #define S64Eor(x, y) ((Boolean)((x) ^ (y)))
  189. #define S64Not(x) ((Boolean)(!(x)))
  190. #define S64BitwiseAnd(x, y) ((x) & (y))
  191. #define S64BitwiseOr(x, y) ((x) | (y))
  192. #define S64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  193. #define S64BitwiseNot(x) (~(x))
  194. #define S64ShiftRight(x, y) ((x) >> (y))
  195. #define S64ShiftLeft(x, y) ((x) << (y))
  196. #define SInt64ToLongDouble(x) ((long double)(x))
  197. #define LongDoubleToSInt64(x) ((SInt64)(x))
  198.  
  199. #define U64Max() ULLONG_MAX
  200. #define U64Add(x, y) ((x) + (y))
  201. #define U64Subtract(x, y) ((x) - (y))
  202. #define U64Multiply(x, y) ((x) * (y))
  203. #define U64Div(x, y) ((x) / (y))
  204. #define U64Mod(x, y) ((x) % (y))
  205. #define U64Set(x) ((UInt64) (x))
  206. #define U64SetU(x) ((UInt64) (x))
  207. #define U32SetU(x) ((UInt32) (x))
  208. #define U64Compare(x, y) ((int)((x) - (y)))
  209. #define U64And(x, y) ((Boolean)((x) && (y)))
  210. #define U64Or(x, y) ((Boolean)((x) || (y)))
  211. #define U64Eor(x, y) ((Boolean)((x) ^ (y)))
  212. #define U64Not(x) ((Boolean)(!(x)))
  213. #define U64BitwiseAnd(x, y) ((x) & (y))
  214. #define U64BitwiseOr(x, y) ((x) | (y))
  215. #define U64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  216. #define U64BitwiseNot(x) (~(x))
  217. #define U64ShiftRight(x, y) ((x) >> (y))
  218. #define U64ShiftLeft(x, y) ((x) << (y))
  219. #define UInt64ToLongDouble(x) ((long double)(x))
  220. #define LongDoubleToUInt64(x) ((UInt64)(x))
  221. #define UInt64ToSInt64(x) ((SInt64)(x))
  222. #define SInt64ToUInt64(x) ((UInt64)(x))
  223.  
  224. #else  
  225.  
  226. EXTERN_API_C( SInt64 )
  227. S64Max                            (void);
  228.  
  229. EXTERN_API_C( SInt64 )
  230. S64Min                            (void);
  231.  
  232. EXTERN_API_C( SInt64 )
  233. S64Add                            (SInt64                 x,
  234.                                  SInt64                 y);
  235.  
  236. EXTERN_API_C( SInt64 )
  237. S64Subtract                        (SInt64                 left,
  238.                                  SInt64                 right);
  239.  
  240. EXTERN_API_C( SInt64 )
  241. S64Negate                        (SInt64                 value);
  242.  
  243. EXTERN_API_C( SInt64 )
  244. S64Absolute                        (SInt64                 value);
  245.  
  246. EXTERN_API_C( SInt64 )
  247. S64Multiply                        (SInt64                 xparam,
  248.                                  SInt64                 yparam);
  249.  
  250. EXTERN_API_C( SInt64 )
  251. S64Divide                        (SInt64                 dividend,
  252.                                  SInt64                 divisor,
  253.                                  SInt64 *                remainder);
  254.  
  255. EXTERN_API_C( SInt64 )
  256. S64Set                            (SInt32                 value);
  257.  
  258. EXTERN_API_C( SInt64 )
  259. S64SetU                            (UInt32                 value);
  260.  
  261. EXTERN_API_C( SInt32 )
  262. S32Set                            (SInt64                 value);
  263.  
  264. EXTERN_API_C( int )
  265. S64Compare                        (SInt64                 left,
  266.                                  SInt64                 right);
  267.  
  268. EXTERN_API_C( Boolean )
  269. S64And                            (SInt64                 left,
  270.                                  SInt64                 right);
  271.  
  272. EXTERN_API_C( Boolean )
  273. S64Or                            (SInt64                 left,
  274.                                  SInt64                 right);
  275.  
  276. EXTERN_API_C( Boolean )
  277. S64Eor                            (SInt64                 left,
  278.                                  SInt64                 right);
  279.  
  280. EXTERN_API_C( Boolean )
  281. S64Not                            (SInt64                 value);
  282.  
  283. EXTERN_API_C( SInt64 )
  284. S64BitwiseAnd                    (SInt64                 left,
  285.                                  SInt64                 right);
  286.  
  287. EXTERN_API_C( SInt64 )
  288. S64BitwiseOr                    (SInt64                 left,
  289.                                  SInt64                 right);
  290.  
  291. EXTERN_API_C( SInt64 )
  292. S64BitwiseEor                    (SInt64                 left,
  293.                                  SInt64                 right);
  294.  
  295. EXTERN_API_C( SInt64 )
  296. S64BitwiseNot                    (SInt64                 value);
  297.  
  298. EXTERN_API_C( SInt64 )
  299. S64ShiftRight                    (SInt64                 value,
  300.                                  UInt32                 shift);
  301.  
  302. EXTERN_API_C( SInt64 )
  303. S64ShiftLeft                    (SInt64                 value,
  304.                                  UInt32                 shift);
  305.  
  306. /*
  307.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  308. */
  309. EXTERN_API_C( long double )
  310. SInt64ToLongDouble                (SInt64                 value);
  311.  
  312. EXTERN_API_C( SInt64 )
  313. LongDoubleToSInt64                (long double             value);
  314.  
  315. EXTERN_API_C( UInt64 )
  316. U64Max                            (void);
  317.  
  318. EXTERN_API_C( UInt64 )
  319. U64Add                            (UInt64                 x,
  320.                                  UInt64                 y);
  321.  
  322. EXTERN_API_C( UInt64 )
  323. U64Subtract                        (UInt64                 left,
  324.                                  UInt64                 right);
  325.  
  326. EXTERN_API_C( UInt64 )
  327. U64Multiply                        (UInt64                 xparam,
  328.                                  UInt64                 yparam);
  329.  
  330. EXTERN_API_C( UInt64 )
  331. U64Divide                        (UInt64                 dividend,
  332.                                  UInt64                 divisor,
  333.                                  UInt64 *                remainder);
  334.  
  335. EXTERN_API_C( UInt64 )
  336. U64Set                            (SInt32                 value);
  337.  
  338. EXTERN_API_C( UInt64 )
  339. U64SetU                            (UInt32                 value);
  340.  
  341. EXTERN_API_C( UInt32 )
  342. U32SetU                            (UInt64                 value);
  343.  
  344. EXTERN_API_C( int )
  345. U64Compare                        (UInt64                 left,
  346.                                  UInt64                 right);
  347.  
  348. EXTERN_API_C( Boolean )
  349. U64And                            (UInt64                 left,
  350.                                  UInt64                 right);
  351.  
  352. EXTERN_API_C( Boolean )
  353. U64Or                            (UInt64                 left,
  354.                                  UInt64                 right);
  355.  
  356. EXTERN_API_C( Boolean )
  357. U64Eor                            (UInt64                 left,
  358.                                  UInt64                 right);
  359.  
  360. EXTERN_API_C( Boolean )
  361. U64Not                            (UInt64                 value);
  362.  
  363. EXTERN_API_C( UInt64 )
  364. U64BitwiseAnd                    (UInt64                 left,
  365.                                  UInt64                 right);
  366.  
  367. EXTERN_API_C( UInt64 )
  368. U64BitwiseOr                    (UInt64                 left,
  369.                                  UInt64                 right);
  370.  
  371. EXTERN_API_C( UInt64 )
  372. U64BitwiseEor                    (UInt64                 left,
  373.                                  UInt64                 right);
  374.  
  375. EXTERN_API_C( UInt64 )
  376. U64BitwiseNot                    (UInt64                 value);
  377.  
  378. EXTERN_API_C( UInt64 )
  379. U64ShiftRight                    (UInt64                 value,
  380.                                  UInt32                 shift);
  381.  
  382. EXTERN_API_C( UInt64 )
  383. U64ShiftLeft                    (UInt64                 value,
  384.                                  UInt32                 shift);
  385.  
  386. /*
  387.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  388. */
  389. EXTERN_API_C( long double )
  390. UInt64ToLongDouble                (UInt64                 value);
  391.  
  392. EXTERN_API_C( UInt64 )
  393. LongDoubleToUInt64                (long double             value);
  394.  
  395. EXTERN_API_C( SInt64 )
  396. UInt64ToSInt64                    (UInt64                 value);
  397.  
  398. EXTERN_API_C( UInt64 )
  399. SInt64ToUInt64                    (SInt64                 value);
  400.  
  401. #endif /* TYPE_LONGLONG */
  402.  
  403. /* 
  404.     Functions to convert between [Unsigned]Wide and [S|U]Int64 types.
  405.     
  406.     These functions are necessary if source code which uses both
  407.     wide and SInt64 is to compile under a compiler that supports
  408.     long long.
  409. */
  410. #if TYPE_LONGLONG 
  411.     #define SInt64ToWide(x)         (*((wide*)(&x)))
  412.     #define WideToSInt64(x)         (*((SInt64*)(&x)))
  413.     #define UInt64ToUnsignedWide(x) (*((UnsignedWide*)(&x)))
  414.     #define UnsignedWideToUInt64(x) (*((UInt64*)(&x)))
  415. #else
  416.     #define SInt64ToWide(x)         (x)
  417.     #define WideToSInt64(x)         (x)
  418.     #define UInt64ToUnsignedWide(x) (x)
  419.     #define UnsignedWideToUInt64(x) (x)
  420. #endif
  421.  
  422.  
  423.  
  424.  
  425. #if PRAGMA_STRUCT_ALIGN
  426.     #pragma options align=reset
  427. #elif PRAGMA_STRUCT_PACKPUSH
  428.     #pragma pack(pop)
  429. #elif PRAGMA_STRUCT_PACK
  430.     #pragma pack()
  431. #endif
  432.  
  433. #ifdef PRAGMA_IMPORT_OFF
  434. #pragma import off
  435. #elif PRAGMA_IMPORT
  436. #pragma import reset
  437. #endif
  438.  
  439. #ifdef __cplusplus
  440. }
  441. #endif
  442.  
  443. #endif /* __MATH64__ */
  444.  
  445.